/*
ID: icerupt1
PROG: race3
LANG: C++11
*/

/* solution
 *
 * good problem.
 * 联通性问题。
 * 第一问只要删除该点看看起点到终点联不联通就行。
 * 第二问一定是第一问的子集，所以对于第一问得到的每个点，先从该点进行dfs，
 * 再从起点进行dfs，如果两次dfs没有相交的点，那么说明该点是个分割点。
 *
*/

#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>

std::ifstream fin {"race3.in" };
std::ofstream fout{"race3.out"};

std::vector<std::vector<int>> graph;
int n = 1;

bool connected(int s, int t, int disable, std::vector<bool> & vis,
					std::vector<std::vector<int>> const & graph)
{
	if (s == t) return true;
	vis[s] = true;
	for (auto it = graph[s].begin(); it != graph[s].end(); ++it) {
		int v = *it;
		if (v == disable) continue;
		if (!vis[v]) {
			if (connected(v, t, disable, vis, graph)) return true;
		}
	}
	return false;
}

int dfs_count(int x, std::vector<bool> & vis)
{
	vis[x] = true;
	int tot = 1;
	for (auto v : graph[x]) {
		if (!vis[v]) {
			tot += dfs_count(v, vis);
		}
	}
	return tot;
}

int main()
{
	for (int t; fin >> t; n++) {
		if (t == -1) break;
		std::vector<int> edge;
		for (; t != -2; fin >> t)
			edge.push_back(t);
		graph.push_back(edge);
	}
	--n;

	std::vector<int> unavoidable;
	for (int i = 1; i < n-1; i++) {
		std::vector<bool> vis;
		vis.resize(n);
		if (!connected(0, n-1, i, vis, graph)) unavoidable.push_back(i);
	}
	std::cout << unavoidable.size();
	fout << unavoidable.size();
	for (auto v : unavoidable) {
		std::cout << ' ' << v;
		fout << ' ' << v;
	}
	std::cout << '\n';
	fout << '\n';

	std::vector<int> splitting;

	for (auto i : unavoidable) {
		std::vector<bool> vis;
		vis.resize(n);
		dfs_count(i, vis);


		std::vector<bool> vis2;
		vis2.resize(n);
		connected(0, n-1, i, vis2, graph);

		bool opt = true;
		for (int i = 0; i < n; i++)
			if (vis[i] && vis2[i]) { opt = false; break; }

		if (opt) splitting.push_back(i);
	}
	std::cout << splitting.size();
	fout << splitting.size();
	for (auto v : splitting) {
		std::cout << ' ' << v;
		fout << ' ' << v;
	}
	std::cout << '\n';
	fout << '\n';
}

